﻿Namespace SetupDatastore
    Public Class Manager
        Public Shared Function SetupAndUpgradeDataStore(ByVal owner As IWin32Window) As Boolean
            If Not DataStore.Setup.IsLocalDatabaseSetup Then
                Dim clsRes As Result = GetPathAndPassword(owner)
                If clsRes Is Nothing Then Return False
                If Not DataStore.Environment.TestConnection(clsRes.Path, clsRes.Password) Then
                    DataStore.Environment.CreateLocalDatabase(clsRes.Path, clsRes.Password)
                    MessageBox.Show("Created local database successfully!")
                Else
                    MessageBox.Show("Connection to local database successful!")
                End If
                DataStore.Environment.SetActiveDatabase(clsRes.Path, clsRes.Password)
            End If

            If Not DataStore.Setup.IsLocalDatabaseUpToDate Then
                Select Case MessageBox.Show("Local database needs to be upgraded! Continue?", "Upgrade", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                    Case DialogResult.Yes
                    Case Else
                        Return False
                End Select
            End If

            DataStore.Setup.SyncronizeDatabases()

            Return True
        End Function


        Private Shared Function GetPathAndPassword(ByVal owner As IWin32Window) As Result
            Return FGetDatastorePathAndPassword.CreateAndShowDialog(owner, DataStore.Environment.DBPath, DataStore.Environment.DBPassword)
        End Function

    End Class

    Friend Class Result
        Public Password As String
        Public Path As String
    End Class

End Namespace
